package controller;

import model.Counter;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/Guess")
public class GuessServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取当前用户会话
        HttpSession session = request.getSession();
        String user = (String) session.getAttribute("user");
        Integer real = (Integer) session.getAttribute("real");
        if (user == null) {
            // 从cookie中获取用户名和随机数
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if ("user".equals(cookie.getName())) {
                    user = cookie.getValue();
                    session.setAttribute("user", user);
                }
                if ("real".equals(cookie.getName())){
                    real = Integer.parseInt(cookie.getValue());
                    session.setAttribute("real", real);
                }
            }
        }

        // 当前用户一局游戏的猜测次数
        Integer count = (Integer) session.getAttribute("count");
        if (count == null) {
            count = 0;
            // 从cookie中获取猜测次数
            Cookie[] cookies = request.getCookies();
            for (Cookie cookie : cookies) {
                if ("count".equals(cookie.getName())) {
                    count = Integer.parseInt(cookie.getValue());
                    session.setAttribute("count", count);
                }
            }
        }

        String num = request.getParameter("num");
        if (num != null) {
            count++;
            session.setAttribute("count", count);

            // 将猜测次数存入cookie
            Cookie countCookie = new Cookie("count", count.toString());
            countCookie.setMaxAge(7 * 24 * 3600);
            response.addCookie(countCookie);

            String result="";
            System.out.println(num);
            if (Integer.parseInt(num) < real)
                result = "你猜的数字("+num+")小了点哦！";
            else if (Integer.parseInt(num) > real)
                result = "你猜的数字("+num+")大了点哦！";
            else{
                // 更新用户猜数的最好成绩
                result = "恭喜你，用了"+count+"次猜对了！（已自动注销，若要进行新的一局请重新登录哦）";
                ServletContext application = request.getServletContext();
                Counter counter = (Counter) application.getAttribute("counter");
                if (counter == null){
                    counter = new Counter();
                    application.setAttribute("counter", counter);
                }
                counter.add(user, count);

                // 一局游戏结束注销当前用户
                session.invalidate();
                Cookie[] cookies = request.getCookies();
                for (Cookie cookie : cookies) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                }
            }
            // 返回猜测结果
            request.setAttribute("result", result);
        }

        RequestDispatcher view = request.getRequestDispatcher(response.encodeURL("guess.jsp"));
        view.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
